package fun.wlfj.j2t.converter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.ConstructorDeclaration;

import fun.wlfj.j2t.container.ClazzContainer;
import fun.wlfj.j2t.model.Clazz;
import fun.wlfj.j2t.model.Constructer;
import fun.wlfj.j2t.model.Element;
import fun.wlfj.j2t.model.Property;

public class IR2AST {
	
	public static List<ClassOrInterfaceDeclaration> job(ClazzContainer container) {
		List<ClassOrInterfaceDeclaration> list = new ArrayList<>();
		for(String name : container.keySet()) {
			ClassOrInterfaceDeclaration curClazz = new ClassOrInterfaceDeclaration();
			// 我们一个一个来创建，事实上这应该是Clazz内部的事情？
			curClazz.setName(name);
			
			Clazz irClazz = container.get(name);
			
			// 下面是属性了
			for(Property proper : irClazz.properList) {
				curClazz.addField(proper.type.name, proper.name);
			}
			
			// 下面是构造器
			for(Constructer cons : irClazz.consList) {
				ConstructorDeclaration mycons = new ConstructorDeclaration();
				
				mycons.setName(cons.name);
				
				// 这里我们要处理一下变量名的问题
				Map<String, Integer> mem = new HashMap<>();
				for(Element p : cons.paramList) {
					String paraName;
					if(mem.containsKey(p.name)) {
						mem.put(p.name, mem.get(p.name) + 1);
						paraName = p.name + mem.get(p.name);
					} else {
						mem.put(p.name, 0);
						paraName = p.name.toLowerCase() + 0;
					}
					mycons.addParameter(p.name, paraName);
				}
				
				curClazz.addMember(mycons);
			}
			list.add(curClazz);
		}
		return list;
	}
	
}
